﻿using Donal.Xms.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.dialog
{
    public partial class dialog_select_premium : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int f;//0 主体（商品或品类） 1对象（会员或组别）
        protected int p;//0 决定是商品或品类，是会员或组别。0为商品或会员，1为品类或组别。
        protected string keywords = string.Empty;
        protected int group_id;
        protected int status;
        protected int invoice;
        protected int monthly;
        protected int products;
        protected string property;
        protected int industry;
        protected int category_id;
        protected string start_time = string.Empty;
        protected string end_time = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.f = DTRequest.GetQueryInt("f");
            this.p = DTRequest.GetQueryInt("p");
            this.category_id = DTRequest.GetQueryInt("category_id");
            this.keywords = DTRequest.GetQueryString("keywords");
            this.group_id = DTRequest.GetQueryInt("group_id");
            this.status = DTRequest.GetQueryInt("status");
            this.property = DTRequest.GetQueryString("property");
            this.monthly = DTRequest.GetQueryInt("monthly");
            this.invoice = DTRequest.GetQueryInt("invoice");
            this.products = DTRequest.GetQueryInt("products");
            this.industry = DTRequest.GetQueryInt("industry");
            this.start_time = DTRequest.GetQueryString("start_time");
            this.end_time = DTRequest.GetQueryString("end_time");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("spec_prices", DTEnums.ActionEnum.View.ToString()); //检查权限

                if (f == 1)
                {
                    if (p == 0)//会员
                    {
                        pagelist.Visible = true;
                        divIsUser.Visible = true;
                        TreeBind("is_lock=0");
                        IndustryBind("is_lock=0");
                        RptBind("status=0" + CombSqlTxt(this.keywords), "id desc");
                    }
                    else//组别
                    {
                        divIsUserGroup.Visible = true;
                        RptGroupBind("id>0" + CombSqlTxt(this.keywords));
                    }
                }
                else
                {
                    if (p == 0)//商品规格
                    {
                        pagelist.Visible = true;
                        divIsGoods.Visible = true;
                        GoodsTreeBind();
                        RptGoodsBind("id>0" + CombGoodsSqlTxt(keywords), "sort_id asc,id desc");
                    }
                    else if (p == 1)//商品
                    {
                        pagelist.Visible = true;
                        divIsProduct.Visible = true;
                        ProductTreeBind();
                        ProductBind("id>0" + ProductCombSqlTxt(keywords), "sort_id asc,id desc");
                    }
                    else//品类
                    {
                        divIsGoodsCategory.Visible = true;
                        BLL.products_category bll = new BLL.products_category();
                        DataTable dt = bll.GetList(0);
                        this.rptCategory.DataSource = dt;
                        this.rptCategory.DataBind();
                    }
                }
            }
        }

        #region 绑定会员

        #region 绑定行业=================================
        private void IndustryBind(string strWhere)
        {
            BLL.user_industry bll = new BLL.user_industry();
            DataTable dt = bll.GetList(0, strWhere, "sort_id,id desc").Tables[0];

            this.ddlIndustry.Items.Clear();
            this.ddlIndustry.Items.Add(new ListItem("所有行业", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlIndustry.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 绑定组别=================================
        private void TreeBind(string strWhere)
        {
            BLL.user_groups bll = new BLL.user_groups();
            DataTable dt = bll.GetList(0, strWhere, "id desc").Tables[0];

            this.ddlGroupId.Items.Clear();
            this.ddlGroupId.Items.Add(new ListItem("会员组别", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlGroupId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;

            if (this.group_id > 0)
            {
                this.ddlGroupId.SelectedValue = this.group_id.ToString();
            }
            if (this.status > 0)
            {
                this.ddlStatus.SelectedValue = this.status.ToString();
            }
            if (this.monthly > 0)
            {
                this.ddlMonthly.SelectedValue = this.monthly.ToString();
            }
            if (this.industry > 0)
            {
                this.ddlIndustry.SelectedValue = this.industry.ToString();
            }
            txtStartTime.Text = start_time;
            txtEndTime.Text = end_time;

            BLL.company bll = new BLL.company();
            this.rptList.DataSource = bll.GetListView(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();

            string pageUrl = Utils.CombUrlTxt("dialog_select_premium.aspx", "group_id={0}&status={1}&monthly={2}&invoice={3}&start_time={4}&end_time={5}&keywords={6}&page={7}&industry={8}&f={9}&p={10}", group_id.ToString(), status.ToString(), monthly.ToString(), invoice.ToString(), start_time.ToString(), end_time.ToString(), this.keywords, "__id__", this.industry.ToString(), this.f.ToString(), this.p.ToString());
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");

            if (group_id > 0)
            {
                strTemp.Append(" and group_id=" + group_id);
            }
            if (status > 0)
            {
                strTemp.Append(" and status=" + status);
            }
            if (monthly > 0)
            {
                strTemp.Append(" and monthly=" + monthly);
            }
            if (invoice > 0)
            {
                strTemp.Append(" and invoiceType=" + invoice);
            }
            if (industry > 0)
            {
                strTemp.Append(" and industry=" + industry);
            }

            if (!string.IsNullOrEmpty(start_time))
            {
                strTemp.Append(" and datediff(d,add_time,'" + start_time + "')<=0");
            }
            if (!string.IsNullOrEmpty(end_time))
            {
                strTemp.Append(" and datediff(d,add_time,'" + end_time + "')>=0");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (companyName like '%" + _keywords + "%' or industry like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("dialog_select_premium_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        #region 返回用户状态=============================
        protected string GetUserStatus(int status)
        {
            string result = string.Empty;
            switch (status)
            {
                case 0:
                    result = "正常";
                    break;
                case 1:
                    result = "正常";
                    break;
                case 2:
                    result = "禁用";
                    break;
                case 3:
                    result = "待审核";
                    break;
            }
            return result;
        }
        #endregion

        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            redirect();
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("dialog_select_premium_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("dialog_select_premium.aspx",
                "group_id={0}&status={1}&monthly={2}&invoice={3}&start_time={4}&end_time={5}&keywords={6}&page={7}&industry={8}&f={9}&p={10}",
                ddlGroupId.SelectedValue, ddlStatus.SelectedValue, ddlMonthly.SelectedValue, "", txtStartTime.Text, txtEndTime.Text, txtKeywords.Text, "__id__", ddlIndustry.SelectedValue, this.f.ToString(), this.p.ToString()));
        }


        protected void ddlGroupId_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlMonthly_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlInvoiceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlIndustry_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        private void redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("dialog_select_premium.aspx",
                "group_id={0}&status={1}&monthly={2}&invoice={3}&start_time={4}&end_time={5}&keywords={6}&industry={7}&f={8}&p={9}",
                ddlGroupId.SelectedValue, ddlStatus.SelectedValue, ddlMonthly.SelectedValue, "", txtStartTime.Text, txtEndTime.Text, txtKeywords.Text, ddlIndustry.SelectedValue, this.f.ToString(), this.p.ToString()));
        }
        #endregion

        #region 绑定组别

        #region 数据绑定=================================
        private void RptGroupBind(string _strWhere)
        {
            this.txtKeywords.Text = this.keywords;
            BLL.user_groups bll = new BLL.user_groups();
            this.rptGroupList.DataSource = bll.GetList(0, _strWhere, "grade asc,id asc");
            this.rptGroupList.DataBind();
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombGroupSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and title like '%" + _keywords + "%'");
            }

            return strTemp.ToString();
        }
        #endregion

        //关健字查询
        protected void btnGroupSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("dialog_select_premium.aspx", "keywords={0}&f={1}&p={2}", txtGroupKey.Text.Trim(), this.f.ToString(), this.p.ToString()));
        }



        #endregion

        #region 绑定商品规格

        private void goodsRedirect()
        {
            Response.Redirect(Utils.CombUrlTxt("dialog_select_premium.aspx",
                "category_id={0}&keywords={1}&f={2}&p={3}&status={4}&products={5}",
                ddlGoodsCategory.SelectedValue, txtGoodsKeywords.Text, this.f.ToString(), this.p.ToString(), ddlGoodsStatus.SelectedValue,ddlGoodsProducts.SelectedValue));
        }

        #region 绑定类别=================================
        private void GoodsTreeBind()
        {
            BLL.products_category bll = new BLL.products_category();
            DataTable dt = bll.GetList(0);

            this.ddlGoodsCategory.Items.Clear();
            this.ddlGoodsCategory.Items.Add(new ListItem("所有类别", ""));

            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlGoodsCategory.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlGoodsCategory.Items.Add(new ListItem(Title, Id));
                }
            }

            ProductsBind();
        }
        #endregion

        #region 绑定产品=================================
        private void ProductsBind()
        {
            this.ddlGoodsProducts.Items.Clear();
            this.ddlGoodsProducts.Items.Add(new ListItem("所有产品", ""));

            if (category_id > 0)
            {
                BLL.products bll = new BLL.products();
                DataSet dt = bll.GetList(0, "category_id in(select id from dt_products_category where class_list like '%," + category_id + ",%')", "sort_id");

                if (Utils.CheckDataSet(dt))
                {
                    foreach (DataRow dr in dt.Tables[0].Rows)
                    {
                        string Id = dr["id"].ToString();
                        string Title = dr["title"].ToString().Trim();
                        this.ddlGoodsProducts.Items.Add(new ListItem(Title, Id));
                    }
                }
            }
        }
        #endregion

        #region 数据绑定=================================
        private void RptGoodsBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            this.ddlGoodsCategory.SelectedValue = category_id.ToString();
            this.ddlGoodsProducts.SelectedValue = this.status.ToString();
            this.ddlGoodsProducts.SelectedValue = this.products.ToString();
            this.txtGoodsKeywords.Text = this.keywords;

            //图表或列表显示
            BLL.products_goods bll = new BLL.products_goods();

            this.rptGoodsList.DataSource = bll.GetListView(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptGoodsList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("dialog_select_premium.aspx",
                "category_id={0}&keywords={1}&f={2}&p={3}&status={4}&products={5}",
                category_id.ToString(), this.keywords, this.f.ToString(), this.p.ToString(), status.ToString(), products.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombGoodsSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords
                    + "%' or sub_title like '%" + _keywords
                    + "%' or category_title like '%" + _keywords
                    + "%' or product_title like '%" + _keywords                                      
                    + "%' or sell_price like '%" + _keywords
                    + "%' or abstracts like '%" + _keywords
                    + "%' or contents like '%" + _keywords + "%')");
            }

            switch (status)
            {
                case 1:
                    strTemp.Append(" and status=0");
                    break;
                case 2:
                    strTemp.Append(" and status=1");
                    break;
            }

            if (products > 0)
            {
                strTemp.Append(" and products_id=" + products);
            }

            if (category_id > 0)
            {
                strTemp.Append(" and category_id in(select id from dt_products_category where class_list like '%," + category_id + ",%')");
            }

            return strTemp.ToString();
        }
        #endregion

        protected void ddlGoodsStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            goodsRedirect();
        }

        protected void ddlGoodsCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlGoodsProducts.SelectedIndex = 0;
            goodsRedirect();
        }

        //关健字查询
        protected void btnGoodsSearch_Click(object sender, EventArgs e)
        {
            goodsRedirect();
        }
        protected void ddlGoodsProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            goodsRedirect();
        }
        #endregion



        #region 绑定商品

        private void productRedirect()
        {
            Response.Redirect(Utils.CombUrlTxt("dialog_select_premium.aspx",
                "category_id={0}&keywords={1}&f={2}&p={3}&status={4}&property={5}",
                ddlProductCategory.SelectedValue, txtProductKey.Text, this.f.ToString(), this.p.ToString(), ddlProductStatus.SelectedValue, ddlProperty.SelectedValue));
        }

        #region 绑定类别=================================
        private void ProductTreeBind()
        {
            BLL.products_category bll = new BLL.products_category();
            DataTable dt = bll.GetList(0);

            this.ddlProductCategory.Items.Clear();
            this.ddlProductCategory.Items.Add(new ListItem("所有类别", ""));

            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                int ClassLayer = int.Parse(dr["class_layer"].ToString());
                string Title = dr["title"].ToString().Trim();

                if (ClassLayer == 1)
                {
                    this.ddlProductCategory.Items.Add(new ListItem(Title, Id));
                }
                else
                {
                    Title = "├ " + Title;
                    Title = Utils.StringOfChar(ClassLayer - 1, "　") + Title;
                    this.ddlProductCategory.Items.Add(new ListItem(Title, Id));
                }
            }
        }
        #endregion

        #region 数据绑定=================================
        private void ProductBind(string _strWhere, string _orderby)
        {
            this.page = DTRequest.GetQueryInt("page", 1);

            this.ddlProductCategory.SelectedValue = category_id.ToString();
            this.ddlProductStatus.SelectedValue = this.status.ToString();
            this.ddlProperty.SelectedValue = this.property;
            this.txtProductKey.Text = this.keywords;

            //图表或列表显示
            BLL.products bll = new BLL.products();
            rptProductList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            rptProductList.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("dialog_select_premium.aspx",
                "category_id={0}&keywords={1}&f={2}&p={3}&status={4}&property={5}&page={6}",
                category_id.ToString(), this.keywords, this.f.ToString(), this.p.ToString(), status.ToString(), property, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string ProductCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (title like  '%" + _keywords
                    + "%' or sub_title like '%" + _keywords
                    + "%' or accuracy_level like '%" + _keywords
                    + "%' or fluid_temperature like '%" + _keywords
                    + "%' or flow_field_sensitivity_level like '%" + _keywords
                    + "%' or climate_and_mechanical_environmental_safety like '%" + _keywords
                    + "%' or electromagnetic_compatibility_level like '%" + _keywords
                    + "%' or power_waste like '%" + _keywords
                    + "%' or output_signal like '%" + _keywords
                    + "%' or communication_interface like '%" + _keywords
                    + "%' or power_supply like '%" + _keywords
                    + "%' or work_environment like '%" + _keywords
                    + "%' or pressure_loss like '%" + _keywords
                    + "%' or data_storage like '%" + _keywords
                    + "%' or execution_standards like '%" + _keywords
                    + "%' or abstracts like '%" + _keywords
                    + "%' or contents like '%" + _keywords + "%')");
            }

            switch (status)
            {
                case 1:
                    strTemp.Append(" and status=0");
                    break;
                case 2:
                    strTemp.Append(" and status=1");
                    break;
            }

            switch (property)
            {
                case "isMsg":
                    strTemp.Append(" and is_msg=1");
                    break;
                case "isTop":
                    strTemp.Append(" and is_top=1");
                    break;
                case "isRed":
                    strTemp.Append(" and is_red=1");
                    break;
                case "isHot":
                    strTemp.Append(" and is_hot=1");
                    break;
                case "isSlide":
                    strTemp.Append(" and is_slide=1");
                    break;
            }

            if (category_id > 0)
            {
                strTemp.Append(" and category_id in(select id from dt_products_category where class_list like '%," + category_id + ",%')");
            }

            return strTemp.ToString();
        }
        #endregion

        protected void rptProductList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(((HiddenField)e.Item.FindControl("hidId")).Value);
            BLL.products bll = new BLL.products();
            Model.products model = bll.GetModel(id);
            switch (e.CommandName)
            {
                case "lbtnIsTop":
                    if (model.is_top == 1)
                        bll.UpdateField(id, "is_top=0");
                    else
                        bll.UpdateField(id, "is_top=1");
                    break;
                case "lbtnIsRed":
                    if (model.is_red == 1)
                        bll.UpdateField(id, "is_red=0");
                    else
                        bll.UpdateField(id, "is_red=1");
                    break;
                case "lbtnIsHot":
                    if (model.is_hot == 1)
                        bll.UpdateField(id, "is_hot=0");
                    else
                        bll.UpdateField(id, "is_hot=1");
                    break;
            }
            productRedirect();
        }

        protected void btnProductSearch_Click(object sender, EventArgs e)
        {
            productRedirect();
        }


        protected void ddlProductStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            productRedirect();
        }

        protected void ddlProductCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            productRedirect();
        }

        protected void ddlProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            productRedirect();
        }

        #endregion

        
    }
}